let questionsApp = new Vue({
    el:"#questionsApp",
    data:{
        questions:[],
        pageSize:8,
        pageInfo:{}
    },
    methods:{
        loadQuestions(pageNum){
            axios.get('/portal/question/teacher?pageNum='+pageNum+'&&pageSize='+this.pageSize)
                .then(result=>{
                    let r = result.data
                    console.log(r)
                    if(r.code==OK){
                        this.pageInfo=r.data
                        this.questions = r.data.list
                        this.updateDuration ()
                        this.updateTagImage()
                    }
                })
        },
        updateTagImage(){
            let questions = this.questions;
            for(let i=0; i<questions.length; i++){
                let tags = questions[i].tags;
                if(tags){
                    let tagImage = '/img/tags/'+tags[0].id+'.jpg';
                    questions[i].tagImage = tagImage;
                }
            }
        },
        updateDuration () {
            let questions = this.questions;
            for(let i=0; i<questions.length; i++){
                //创建问题时候的时间毫秒数
                let createtime = new Date(questions[i].createtime).getTime();
                //当前时间毫秒数
                let now = new Date().getTime();
                let duration = now - createtime;
                if (duration < 1000*60){ //一分钟以内
                    questions[i].duration = "刚刚";
                }else if(duration < 1000*60*60){ //一小时以内
                    questions[i].duration =
                        (duration/1000/60).toFixed(0)+"分钟以前";
                }else if (duration < 1000*60*60*24){
                    questions[i].duration =
                        (duration/1000/60/60).toFixed(0)+"小时以前";
                }else if(duration < 1000*60*60*24*30){
                    questions[i].duration =
                        (duration/1000/60/60/24).toFixed(0)+"天以前";
                }else {
                    questions[i].duration =
                        (duration/1000/60/60/24/30/12).toFixed(0)+"年以前";
                }
            }
        }

    },
    created(){
        this.loadQuestions(1)

    }

})